#!/bin/bash
#Shell菜单演示
basepath=$(cd `dirname $0`; pwd)
function menu ()
{
 cat << EOF
----------------------------------------
|********Centos6/7 Set Static IP********|
----------------------------------------
`echo -e "\033[47;30m 1)DHCP\033[0m"`
`echo -e "\033[47;30m 2)BDIDC_Test\033[0m"`
`echo -e "\033[47;30m 3)Manual\033[0m"`
`echo -e "\033[47;30m 4)ip address show\033[0m"`
`echo -e "\033[47;30m 5)diplay config-file\033[0m"`
`echo -e "\033[47;30m 6)vi ethernet\033[0m"`
`echo -e "\033[47;30m 7)nmcli connection show\033[0m"`
`echo -e "\033[47;30m 8)restart network\033[0m"`
`echo -e "\033[47;30m 9)input command\033[0m"`
`echo -e "\033[47;30m 0)Exit\033[0m"`
EOF
read -p "Input your select:" num1
while [ $num1 != '0' ];do
case $num1 in
 1)
#list interface
ip address show|grep mtu|awk -F: '{print $2}'
read -p "interface:" interface
#modify IP
if [[ -e /etc/sysconfig/network-scripts/ifcfg-$interface ]];then
   cp /etc/sysconfig/network-scripts/ifcfg-$interface /etc/sysconfig/network-scripts/ifcfg-$interface.$(date "+%Y-%m-%d_%H:%M")
   sed -i "/^BOOTPROTO/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^ONBOOT/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPADDR/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^NETMASK/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPPREFIX/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^GATEWAY/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS1/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS2/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   cat >> /etc/sysconfig/network-scripts/ifcfg-$interface << EOF
BOOTPROTO=dhcp
EOF
   echo ""
   echo /etc/sysconfig/network-scripts/ifcfg-$interface
   egrep "IPADDR|NETMASK|GATEWAY|DNS" /etc/sysconfig/network-scripts/ifcfg-$interface
   systemctl restart network 2> /dev/null && systemctl restart NetworkManager || service network restart
   else
   echo "/etc/sysconfig/network-scripts/ifcfg-$interface file is not exist"
fi
exit 0
   ;;
 2)
#set var
#ip1=111.63.48
#mask=255.255.255.248

ip1=111.63.48
mask=255.255.255.248
dns1=111.11.1.1
dns2=111.11.11.1
#list interface
ip address show|grep mtu|awk -F: '{print $2}'
read -p "interface:" interface
clear
 cat << EOF
`echo -e "\033[47;30m ******************************\033[0m"`
`echo -e "\033[47;30m *BDIDC set ip for Centos6/7  *\033[0m"`
`echo -e "\033[47;30m *$ip1.ip                *\033[0m"`
`echo -e "\033[47;30m *$ip1.gateway           *\033[0m"`
`echo -e "\033[47;30m *mask=$mask        *\033[0m"`
`echo -e "\033[47;30m *dns1=$dns1             *\033[0m"`
`echo -e "\033[47;30m *dns2=$dns2            *\033[0m"`
`echo -e "\033[47;30m ******************************\033[0m"`
EOF
echo ""
read -p "input ip:" ip2
read -p "input gateway:" gateway
#modify IP
if [[ -e /etc/sysconfig/network-scripts/ifcfg-$interface ]];then
   cp /etc/sysconfig/network-scripts/ifcfg-$interface /etc/sysconfig/network-scripts/ifcfg-$interface.$(date "+%Y-%m-%d_%H:%M")
   sed -i "/^BOOTPROTO/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^ONBOOT/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPADDR/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^NETMASK/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPPREFIX/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^GATEWAY/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS1/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS2/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   cat >> /etc/sysconfig/network-scripts/ifcfg-$interface << EOF
BOOTPROTO=static
ONBOOT=yes
IPADDR=$ip1.$ip2
NETMASK=$mask
GATEWAY=$ip1.$gateway
DNS1=$dns1
DNS2=$dns2
EOF
   echo ""
   echo /etc/sysconfig/network-scripts/ifcfg-$interface
   egrep "IPADDR|NETMASK|GATEWAY|DNS" /etc/sysconfig/network-scripts/ifcfg-$interface
   systemctl restart network 2> /dev/null && systemctl restart NetworkManager && nmcli connection up $interface || service network restart
   else
   echo "/etc/sysconfig/network-scripts/ifcfg-$interface file is not exist"
fi
exit 0
   ;;
 3)

#list interface
ip address show|grep mtu|awk -F: '{print $2}'
read -p "interface:" interface
read -p "IP Address:" ip
read -p "mask:" mask
read -p "Gateway:" gateway
read -p "DNS1:" dns1
read -p "DNS2:" dns2
clear
 cat << EOF
`echo -e "\033[47;30m ******************************\033[0m"`
`echo -e "\033[47;30m *BDIDC set ip for Centos6/7  *\033[0m"`
`echo -e "\033[47;30m *$ip                *\033[0m"`
`echo -e "\033[47;30m *$gateway           *\033[0m"`
`echo -e "\033[47;30m *mask=$mask        *\033[0m"`
`echo -e "\033[47;30m *dns1=$dns1             *\033[0m"`
`echo -e "\033[47;30m *dns2=$dns2            *\033[0m"`
`echo -e "\033[47;30m ******************************\033[0m"`
EOF
#modify IP
if [[ -e /etc/sysconfig/network-scripts/ifcfg-$interface ]];then
   cp /etc/sysconfig/network-scripts/ifcfg-$interface /etc/sysconfig/network-scripts/ifcfg-$interface.$(date "+%Y-%m-%d_%H:%M")
   sed -i "/^BOOTPROTO/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^ONBOOT/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPADDR/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^NETMASK/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^IPPREFIX/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^GATEWAY/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS1/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   sed -i "/^DNS2/d" /etc/sysconfig/network-scripts/ifcfg-$interface
   cat >> /etc/sysconfig/network-scripts/ifcfg-$interface << EOF
BOOTPROTO=static
ONBOOT=yes
IPADDR=$ip
NETMASK=$mask
GATEWAY=$gateway
DNS1=$dns1
DNS2=$dns2
EOF
   echo ""
   echo /etc/sysconfig/network-scripts/ifcfg-$interface
   egrep "IPADDR|NETMASK|GATEWAY|DNS" /etc/sysconfig/network-scripts/ifcfg-$interface
  systemctl restart network 2> /dev/null && systemctl restart NetworkManager && nmcli connection up $interface || service network restart
   else
   echo "/etc/sysconfig/network-scripts/ifcfg-$interface file is not exist"
fi
exit 0
   ;;
 4)
   ip a |egrep --color "state|inet"
   menu
   ;;
 5)
ls -n /etc/sysconfig/network-scripts/ifcfg-*
   menu
   ;;
 6)
#list interface
ip address show|grep mtu|awk -F: '{print $2}'
read -p "interface:" interface
vi /etc/sysconfig/network-scripts/ifcfg-$interface
  menu
  ;;
 7)
nmcli -f name,device,active,autoconnect,autoconnect-priority connection
  menu
  ;;
 8)
systemctl restart network 2> /dev/null && systemctl restart NetworkManager && nmcli connection up $interface || service network restart
  menu
  ;;
 9)
read -p "input command:" command
$command
  menu
  ;;
 0)
   exit 0
esac
done
}
menu






